//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.medical.device.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public SpringUtils() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtils.applicationContext == null) {
            SpringUtils.applicationContext = applicationContext;
        }

        System.out.println("----------------------------------------------------------------------");
        System.out.println("Swagger测试地址:");

        try {
            String port = applicationContext.getEnvironment().getProperty("server.port");
            String path = applicationContext.getEnvironment().getProperty("server.servlet.context-path");
            if (path != null && path.startsWith("/")) {
                path = path.substring(1);
            }

            if (path != null && path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }

            System.out.println("http://127.0.0.1:" + port + "/" + path + "/swagger-ui.html");
            System.out.println();
//            List<String> localIPList = getLocalIPList();
//            String ip = (String)localIPList.stream().filter((x) -> {
//                return x.startsWith("192.168.2.");
//            }).findFirst().get();
//            System.out.println("http://" + ip + ":" + port + "/" + path + "/swagger-ui.html");
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        System.out.println();
        System.out.println("----------------------------------------------------------------------");
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    public static String getActiveProfile() {
        return applicationContext.getEnvironment().getActiveProfiles()[0];
    }

    public static boolean isProdEnvironment() {
        return "prod".equalsIgnoreCase(getActiveProfile());
    }

    public static List<String> getLocalIPList() {
        ArrayList ipList = new ArrayList();

        try {
            Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();

            while(networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface)networkInterfaces.nextElement();
                Enumeration inetAddresses = networkInterface.getInetAddresses();

                while(inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress)inetAddresses.nextElement();
                    if (inetAddress != null && inetAddress instanceof Inet4Address) {
                        String ip = inetAddress.getHostAddress();
                        ipList.add(ip);
                    }
                }
            }
        } catch (SocketException var6) {
            var6.printStackTrace();
        }

        return ipList;
    }
}
